package com.nuanshui.heatedloan.baseservice.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nuanshui.framework.annotation.search.Document;
import com.nuanshui.framework.annotation.search.SearchField;
import com.nuanshui.framework.utils.UniqueNoUtils.UniqueNoType;
import com.nuanshui.heatedloan.baseservice.service.Archivable;
import com.nuanshui.heatedloan.dal.BaseEntityImpl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;


@SuppressWarnings("rawtypes")
@Document(index="users",type="user")
public class User extends BaseEntityImpl implements UserDetails, Archivable,Comparable {

	private static final long serialVersionUID = 1L;

	/** 后台管理员 */
	public static final long ROLE_ADMIN = 1;

	private static final SimpleGrantedAuthority role_auth_admin = new SimpleGrantedAuthority(
			"ROLE_ADMIN");

	private static final SimpleGrantedAuthority role_auth_union = new SimpleGrantedAuthority(
			"ROLE_UNION");
	
	@SearchField
	private String nick;

	private String avatar;
	@JsonIgnore
	@JSONField(serialize=false)
	private String loginname;
	@JsonIgnore
	@JSONField(serialize=false)
	private String phone;

	@JsonIgnore
	@JSONField(serialize=false)
	private String password;
	@JsonIgnore
	@JSONField(serialize=false)
	private Boolean archive;

	// 1:普通用户、2:达人、-1:运营
	private String admin;

	// 纬度坐标
	@JsonIgnore
	private String latitude;

	// 经度坐标
	@JsonIgnore
	@JSONField(serialize=false)
	private String longitude;
	
	private String signature;
	@JsonIgnore
	@JSONField(serialize=false)
	private Boolean recommended;
	@JsonIgnore
	@JSONField(serialize=false)
	private Integer notReadNum;
	@JsonIgnore
	@JSONField(serialize=false)
	private String series;
	
	private String location;
	
	private boolean isNew;
	
	private String chatToken;
	
	private Integer sex;//0 男 1 女
	private String  address;
	private String userTencentToken;
	@JsonIgnore
	@JSONField(serialize=false)
	private Long auth;
	
	
	
	public void setAuth(Long auth) {
		this.auth = auth;
	}
	public String getUserTencentToken() {
		return userTencentToken;
	}
	public void setUserTencentToken(String userTencentToken) {
		this.userTencentToken = userTencentToken;
	}
	public Integer getSex() {
		if(sex == null) return 2;
		return sex;
	}
	public void setSex(Integer sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	

	public String getChatToken() {
		return chatToken;
	}

	public void setChatToken(String chatToken) {
		this.chatToken = chatToken;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public boolean getIsNew() {
		return isNew;
	}

	public void setIsNew(boolean isNew) {
		this.isNew = isNew;
	}

	public String getSeries() {
		return series;
	}

	public void setSeries(String series) {
		this.series = series;
	}

	public Integer getNotReadNum() {
		return notReadNum;
	}

	public void setNotReadNum(Integer notReadNum) {
		this.notReadNum = notReadNum;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>(2);
		if("-1".equals(this.admin)){
			auths.add(role_auth_admin);
		}
		auths.add(role_auth_union);
		return auths;
	}
	@JsonIgnore
	@JSONField(serialize=false)
	public Long getAuth() {
		return auth;
	}
	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public String getUsername() {
		return this.getLoginname();
	}
	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	@JsonIgnore
	@JSONField(serialize=false)
	public boolean isEnabled() {
		return true;
	}

	@Override
	public Boolean getArchive() {
		return archive;
	}

	@Override
	public void setArchive(Boolean archive) {
		this.archive = archive;
	}

	@Override
	public String getId() {
		return super.getId();
	}

	@JsonIgnore
	@JSONField(serialize=false)
	public boolean isAnonymous() {
		return this.loginname.startsWith(UniqueNoType.CID.name());
	}

	public String getAdmin() {
		return admin;
	}

	public void setAdmin(String admin) {
		this.admin = admin;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public Boolean getRecommended() {
		return recommended;
	}

	public void setRecommended(Boolean recommended) {
		this.recommended = recommended;
	}

	@Override
	public int compareTo(Object o) {
		return 0;
	}

	
	
	
	
	
	
	

}